package com.atguigu.gmall.product.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SkuAttrValue;
import com.atguigu.gmall.product.entity.SkuImage;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * 获取SpuInfo
 */
@RestController
@RequestMapping("/api/inner/rpc/product")
public class SkuRpcController {
    @Autowired
    SkuInfoService  skuInfoService;
    @GetMapping("/skuinfo/{skuId}")
    public Result<SkuInfo> getSkuInfo(@PathVariable("skuId") Long skuId){
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        return  Result.ok(skuInfo);
    }

    /**
     * 获取sku-images
     * @param skuId
     * @return
     */
    @Autowired
    SkuImageService  skuImageService;
    @GetMapping("/skuImages/{skuId}")
    public Result<List<SkuImage>> getImages(@PathVariable("skuId") Long skuId){
        List<SkuImage> list = skuImageService.lambdaQuery().eq(SkuImage::getSkuId, skuId)
                .list();
        return  Result.ok(list);
    }

    /**
     * 获取实时价格
     * @return
     */
    @GetMapping("/getprice/{skuId}")
    public Result<BigDecimal> getPrice(@PathVariable("skuId") Long skuId){
        SkuInfo one = skuInfoService.lambdaQuery().select(SkuInfo::getPrice).eq(SkuInfo::getId, skuId).one();
        return  Result.ok(one.getPrice());

    }

    /**
     * 获取销售属性
     * @param spuId
     * @return
     */
    @Autowired
    SpuSaleAttrService  spuSaleAttrService;
    @GetMapping("/skuSaleAttr/{spuId}/{skuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttr(@PathVariable("spuId") Long spuId,@PathVariable("skuId") Long skuId){
     List<SpuSaleAttr> saleAttrs= spuSaleAttrService.getSpuSaleAttrListOrder(spuId,skuId);
        return  Result.ok(saleAttrs);

    }
    @GetMapping("/valueSkujson/{spuId}")
    public Result<String> getValueSkuJson(@PathVariable("spuId") Long spuId ){
        String json =  spuSaleAttrService.getValuesSkuJson(spuId);
        return Result.ok(json);
    }
}
